package Result;
import java.util.ArrayList;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import Lucene.SearchEngine;


public class GetFile {
	private String filename = "";	//filename
	private SearchEngine SE = new SearchEngine();
	private int limit;//maximum match list length
	private ArrayList<String> table = new ArrayList<String>();
	private long ininIndex = 0;
	private long lastIndex = 0;
	private String currentID = "";
	
	
	public GetFile(String filename,int limit)	//create empty file(delete first if it is exist)
	{
		this.limit = limit;
		this.filename = filename + ".csv";
		File file = new File(this.filename);
		if(file.exists())								
			file.delete();
		try {
			FileWriter fw = new FileWriter(file,true);
			fw.write("id,documents\n");
			fw.close();
		} 
		catch (IOException e) {e.printStackTrace();}
	}
	//search the string from the directory path
	/*
	 * input: searchStr -- which string to search
	 * 		  dirPath   -- which directory to get index file
	 * */
	private ArrayList<String> _001_Search(String searchStr,String dirPath_index)
	{
		ArrayList<String> list = new ArrayList<String>();
		try {
			list = SE.ACT001_getSearchResult(searchStr, dirPath_index);//search
		} 
		catch (Exception e) {e.printStackTrace();}
		return list;
	}
	private void initData()
	{
		this.ininIndex = 0;
		this.lastIndex = 0;
		this.table.clear();
	}
	private void writeLines()
	{
		File file = new File(this.filename);
		FileWriter fw;
		
		try 
		{
			fw = new FileWriter(file,true);
			if(this.table.size()<=this.limit)
			{
				fw.write(this.currentID+":"+this.ininIndex+"-"+this.lastIndex+",");
			////////////////
				//System.out.println(this.currentID+":"+this.ininIndex+"-"+this.lastIndex+this.table);
				for(int i = 0; i<this.table.size();i++)
				{
					fw.write(table.get(i));
					if(i!=this.table.size()-1)
						fw.write(" ");
				}
				fw.write("\n");
			}
			fw.close();
		}
		catch (IOException e) {e.printStackTrace();}
	}
	
	
	/*********************************************************/
	/**input : searchPath -- where index file find
	 * 	       searchStr  -- which string to search
	 *         ID		  -- which ID
	 *         number     -- token num
	 */
	/**output: result store .csv file						**/
	/*********************************************************/
	public void ACT_001_SearchAllandWrite(String searchPath ,String searchStr ,String ID ,long number)
	{
		ArrayList<String> list = new ArrayList<String>();
		ArrayList<String> listTemp = new ArrayList<String>();
		list = _001_Search(searchStr,searchPath);//search results
		int length = list.size();	//list size ,0 means no search result
		for(int i =0;i<length; i++)
			listTemp.add(list.get(i));
		//copy list to list_temp
		listTemp.retainAll(this.table);
		boolean hasIntersection = true;
		if(listTemp.isEmpty())
			hasIntersection = false;
		boolean isTableEmpty = false;
		if(this.table.size() == 0)
			isTableEmpty = true;
		System.out.println(number+" "+list+this.table);
		if(this.currentID == "")//first search
		{
			if(length==0)
			{
				//System.out.println("case 0"+list+this.table);
				this.lastIndex = -2;

			}
			else
			{
				//System.out.println("case 1"+list+this.table);
				this.table = list;
			}
			this.currentID = ID;
		}
		else					//other times search
		{
			if(this.currentID == ID)//same id
			{
				
				if(this.lastIndex == number-1 && hasIntersection)//conti and intersection
				{
					//System.out.println("case 2");
					this.table = listTemp;
					this.lastIndex = number;
				}
				else if(this.lastIndex != number-1)
				{
					
					if(length == 0)
					{
						//System.out.println("case 3");
						if(!isTableEmpty)
							writeLines();
						this.table.clear();
						this.ininIndex = number;
						this.lastIndex = number;
					}
					else
					{
						//System.out.println("case 4");
						if(!isTableEmpty)
							writeLines();
						this.table = list;
						this.ininIndex = number;
						this.lastIndex = number;
					}
				}
				else if(!hasIntersection)//continuous but have no intersection
				{
					if(!isTableEmpty)//Table is not empty
					{
						//System.out.println("case 5");
						writeLines();
						this.table = list;
						this.ininIndex = number;
						this.lastIndex = number;
					}
					else if(length == 0)//Table is not empty and search list is no result
					{
						//System.out.println("case 5.1");
						this.ininIndex = 0;
						this.lastIndex = 0;
					}
					else	//Table and list are empty
					{
						//System.out.println("case 5.2");
						this.table = list;
						this.ininIndex = number;
						this.lastIndex = number;
					}
				}
				else	//using for some exception
				{
					System.out.println("case 6"+list+this.table);
				}
				
			}
			else	//different IDs
			{
				if(length>0)	//search list not empty
				{
					if(!isTableEmpty)
					{
						//System.out.println("case 7");
						writeLines();
						this.currentID = ID;
						this.lastIndex = number;
						this.ininIndex = number;
						this.table = list;
					}
					else
					{
						//System.out.println("case 8");
						this.currentID = ID;
						this.lastIndex = number;
						this.ininIndex = number;
						this.table = list;
					}
				}
				else
				{
					//System.out.println("else");
				}
				
				//this.savedIndex = number;
				
				
			}
			/*else	//same id and has intersection
			{
				this.table = listTemp;
				this.lastIndex = number;
			}*/
		
		
		}
		//searchPath;
		
		/*if(number != -1)
		{
			list = _001_Search(searchStr,searchPath);//search results
		
		
		
			if(ID == this.savedID ) //same id
			{
				ArrayList<String> list2 = new ArrayList<String>();
				list2.addAll(list);
				boolean hasSameElement = list2.retainAll(this.table);//get the intersection
				if( list.size() != 0 && (list.equals(table)||hasSameElement) && list2.size() != 0 && number == this.lastIndex+1)//search the same result = match last
				{
					this.lastIndex = number;
					this.table = list2;
					//System.out.println("Conti "+number);
				}
				else if (!list.equals(table)) //different list = not the same result
				{
					if(list.size() < this.limit)
					if(endline)
					{
						_002_writeHead(ID ,number);
						this.table = list;
						this.lastIndex = number ;
						//System.out.println("diff head"+number+" "+table.size());
					}
					else
					{
						_003_writeTail();
						this.table = list;
						if(table.size()!=0)
						{
							_002_writeHead(ID ,number);
							this.lastIndex = number ;
						}
					//System.out.println("diff tail"+number+" "+table.size());
				}
				
			}
			
		}
		else if(ID != this.savedID) //id different
		{
			if(list.size() != 0 && endline == false)
			{
				
				_003_writeTail();
				this.table = list;
				this.lastIndex = number;
				this.savedID = ID;
				if(list.size()!=0)
					_002_writeHead(ID , number);
				//System.out.println("id diff "+number+" "+table.size());
			
			}
			else if (list.size() != 0 && endline == true)
			{
				this.table = list;
				this.lastIndex = number;
				this.savedID = ID;
				
				_002_writeHead(ID , number);
				
				//System.out.println("id diff end"+number+" "+table.size());
			}
			
		}
		}
		else if(endline == false)
			_003_writeTail();*/
	}
	public static void main(String[] args) 
	{
		
	}

}